import 'dart:async';
import 'dart:io';

import 'package:check_app_install/check_app_install.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _checkAppInstallPlugin = CheckAppInstall();

  bool? _installed;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    try {
      if (Platform.isAndroid) {
        // _installed = await _checkAppInstallPlugin.checkIsInstallApp('com.tst.tinsecret');
      } else if (Platform.isIOS) {
        // _installed = await _checkAppInstallPlugin.checkIsInstallApp('xxx://');
      }
    } on PlatformException {
      _installed = false;
    }
    if (!mounted) return;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text('isInstalled: $_installed\n'),
              ElevatedButton(
                onPressed: () {
                  initPlatformState();
                },
                child: const Text('是否安装APP'),
              ),
              ElevatedButton(
                onPressed: () {
                  _checkAppInstallPlugin.openOtherApp(
                      // packageName: 'com.tst.tinsecret'
                      // className: 'com.tinghao123.tstmall.MainActivity'
                      );
                },
                child: const Text('打开其他APP'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
